package com.tnsw.noxnox;

import com.tnsw.coreutils.AsyncManager;
import com.tnsw.coreutils.text.TextUtils;
import com.tnsw.noxnox.db.DBManager;
import com.tnsw.noxnox.db.tables.DownloadProgress;
import com.tnsw.noxnox.downloader.DLBase;
import com.tnsw.noxnox.downloader.DLInterface;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

public class CatalogSingleView implements DLInterface {

	private String tag = "CatalogSingleView";
	private DBManager dbm;
	private AsyncManager asyncManager;
	
	private LayoutInflater inflater;
	private Context context;
	private DownloadProgress dp;
	private BaseAdapter adapter;
	
	private View rootView;
	private View v;
	private TextView title, lblDuration, lblArtist;
	private ImageView thumbImage;
	private ProgressBar progressBar1;	
	private Bitmap bmpThumbnail = null;
	
	public CatalogSingleView(Context context, DownloadProgress dp, BaseAdapter adapter) {
		Log.d(tag, "Constructor(dp=" + dp.primarykey + ")");
		
		this.context = context;
		this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		this.dp = dp;
		this.adapter = adapter;
		
		this.dbm = DBManager.getInstance(context);
		this.asyncManager = AsyncManager.getInstance();
		
		this.createView();
		adapter.notifyDataSetChanged();
	}
	
	public void refreshDp(DownloadProgress dp) {
		this.dp = dp;
	}
	
	private void createView() {
		this.v = this.inflater.inflate(R.layout.list_row, null);
		
		this.title = (TextView) this.v.findViewById(R.id.title);
		this.thumbImage = (ImageView) this.v.findViewById(R.id.list_image);
		this.progressBar1 = (ProgressBar) this.v.findViewById(R.id.progressBar1);
		this.lblDuration = (TextView) this.v.findViewById(R.id.duration);
		this.lblArtist = (TextView) this.v.findViewById(R.id.artist);

		this.setValues();
	}
	
	private void setValues() {
		this.title.setText("(" + String.valueOf(this.dp.primarykey) + ") " + this.dp.title);
		String durationStr = TextUtils.getDuration1(this.dp.duration_sec);
		this.lblDuration.setText(durationStr);
		
		if (this.dp.status == 2) {
			this.progressBar1.setVisibility(View.INVISIBLE);
		} 
		this.handleThumbnail();
		
		String contentType = this.dbm.getDownloadAttributeValue(this.dp.primarykey, "content-type");
		double sizeMb = this.dp.content_length / (1024*1024);
		this.lblArtist.setText(contentType + ", " + String.valueOf(sizeMb) + "MB");
	}
	
	private void handleThumbnail() {
		if (this.bmpThumbnail == null) {
			if (dp.thumbnail_filename != null) {
				if (dp.thumbnail_filename != null) {
					this.bmpThumbnail = BitmapFactory.decodeFile(dp.thumbnail_filename);
					
				}
			}	
		}
		
		
		if (this.bmpThumbnail != null) {
			this.thumbImage.setImageBitmap(this.bmpThumbnail);	
		}		
	}
		
	public View getView() {
		this.setValues();

		return(this.v);
	}
	
	public DownloadProgress getDownloadProgress() {
		return(this.dp);
	}

	@Override
	public void onProgressUI(DLBase sender) {
		
		this.progressBar1.setMax((int) sender.getMax());
		this.progressBar1.setProgress(sender.getCurrentPosition());
		
		this.lblDuration.setText(sender.getTextStatus());	
		
		this.adapter.notifyDataSetChanged();
		//Log.d(tag, "onProgressUI()");		
		
		//boolean b = AsyncManager.isUiThread();
		//Log.d(tag, "OnProgressUI, isUiThread()=" + b);		
	}

	@Override
	public void onProgressBG(DLBase  sender) {
		//boolean b = AsyncManager.isUiThread();
		//Log.d(tag, "OnProgressBG, isUiThread()=" + b);
		
	}

	@Override
	public void onCompletedUI(DLBase sender, long result) {

		
	}

	@Override
	public void onStartedBG(DLBase sender) {
		
	}

	@Override
	public void onError(DLBase sender, boolean isBackground, String message, Exception e) {
		
	}
	
	
}
